//
//  Sample1ViewController.m
//  Sample1
//
//  Created by Atsuto Suyama on 10/07/04.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "Sample1ViewController.h"

@implementation Sample1ViewController

- (IBAction)pushMe {
	NSLog(@"text = %@", myTextView.text);
	NSString* myText = myTextView.text;
	if ([myText isEqualToString:@""]) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"メール本文を入力してください" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	NSRange range = {[myText length]-1,1};
	NSLog(@"saigo = %@",[myText substringWithRange:range]);
	if ([[myText substringWithRange:range] isEqualToString:@"\n"]) {
		NSLog(@"match");
	}else {
		NSLog(@"not match. add kaigyo");
		myText = [myText stringByAppendingString:@"\n"];
	}
	NSLog(@"myText = %@",myText);
	NSArray* myAry = [myText componentsSeparatedByString:@"\n"];
	NSString* myText3 = @"";
	
	for (NSInteger i = 1; i<[myAry count]; i++) {
		NSLog(@"i = %d",i);
		
		NSInteger myInt = rand()%7;
		NSString* kaomoji = @"()\n";
		switch (myInt) {
			case 0:
				kaomoji = @"ｷﾀ━(ﾟ∀ﾟ)━!\n";
				break;
			case 1:
				kaomoji = @"(´∀｀)\n";
				break;
			case 2:
				kaomoji = @"(・∀・)\n";
				break;
			case 3:
				kaomoji = @"(´・ω・｀)\n";
				break;
			case 4:
				kaomoji = @"(；´Д｀)\n";
				break;
			case 5:
				kaomoji = @"(　^ω^)\n";
				break;
			case 6:
				kaomoji = @"Σ(ﾟДﾟ;)\n";
				break;
			default:
				kaomoji = @"(^_^)\n";
				break;
		}
		myText3 = [NSString stringWithFormat:@"%@%@%@",myText3, [myAry objectAtIndex:i-1], kaomoji];
	}
	//NSString* myText2 = [myText stringByReplacingOccurrencesOfString:@"\n" withString:kaomoji];
	NSLog(@"replace = %@", myText3);
	//myTextView.text = myText3;
	
	
	// allocatind new message composer window
	MFMailComposeViewController *mc = [[MFMailComposeViewController
										alloc] init];
	
	// setting a delegate method to "self"
	mc.mailComposeDelegate = self;
	
	// pre-populating the message subject
	[mc setSubject:@""];
	
	[mc setMessageBody:myText3 isHTML:NO];
	
	[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

	[self presentModalViewController:mc animated:YES];
	
	// releasing the controller
	[mc release];
}


// delegate function callback
- (void)mailComposeController:(MFMailComposeViewController
							   *)controller didFinishWithResult:(MFMailComposeResult)result
						error:(NSError *)error {
	// switchng the result
	switch (result) {
		case MFMailComposeResultCancelled:
			NSLog(@"Mail send canceled.");
			/*
			 Execute your code for canceled event here ...
			 */
			break;
		case MFMailComposeResultSaved:
			NSLog(@"Mail saved.");
			/*
			 Execute your code for email saved event here ...
			 */
			break;
		case MFMailComposeResultSent:
			NSLog(@"Mail sent.");
			/*
			 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mail
			 status" message:@"Mail has been sent."  delegate:self
			 cancelButtonTitle:@"Cancel",otherButtonTitles: @"OK", nil];
			 [alert show];
			 [alert release];			
			 */
			/*
			 Execute your code for email sent event here ...
			 */
			break;
		case MFMailComposeResultFailed:
			NSLog(@"Mail send error: %@.", [error localizedDescription]);
			/*
			 Execute your code for email send failed event here ...
			 */
			break;
		default:
			break;
	}
	// hide the modal view controller
	[self dismissModalViewControllerAnimated:YES];
}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[myTextView becomeFirstResponder];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
